//
//  DataSourceManage.m
//  Enote
//
//  Created by wangkejie on 2016/10/21.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "DataSourceManage.h"

static NSString * const modelName = @"Enote";
static NSString * const entityName = @"Enote";
static NSString * const sqliteName = @"Enote";//*.sqlite

@interface DataSourceManage ()



@end

@implementation DataSourceManage

static DataSourceManage * dataSourceManage = nil;
+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataSourceManage = [[DataSourceManage alloc] init];
    });
    
    return dataSourceManage;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (dataSourceManage == nil) {
            dataSourceManage = [super allocWithZone:zone];
        }
    });
    
    return dataSourceManage;
}

- (instancetype)init
{
    if (self = [super init]) {
        [self initUploadCoreData];
    }
    return self;
}

- (void)initUploadCoreData
{
    _coreDataEntityName = entityName;
    _coreDataModelName = modelName;
    _coreDataSqlPath = sqliteName;
    
    _coreDataApi = [[CoreDataAPI alloc] initWithCoreData:self.coreDataEntityName modelName:self.coreDataModelName sqlPath:self.coreDataSqlPath success:^{
        NSLog(@"initUploadCoreData success");
    } fail:^(NSError *error) {
        NSLog(@"initUploadCoreData fail");
    }];
    
    _context = _coreDataApi.context;
}

#pragma mark - -- 添加
- (void)insertEnoteModel:(Enote *)model success:(void(^)(void))success fail:(void(^)(NSError *error))fail
{
    NSDictionary * dict = model.getObjInfoDic;
    
    [self.coreDataApi insertNewEntity:dict success:^{
        if (success) {
            success();
        }
    } fail:^(NSError *error) {
        if (fail) {
            fail(error);
        }
    }];
}

#pragma mark - -- 更新记录
- (void)updateEnoteModel:(Enote *)model success:(void(^)(void))success fail:(void(^)(NSError *error))fail
{
    __block Enote * blockModel = model;
    NSString * filterStr = [NSString stringWithFormat:@"creatTimeOnly = %lld",model.creatTimeOnly];
    [self.coreDataApi readEntity:nil ascending:YES filterStr:filterStr success:^(NSArray *results) {
        if (results.count > 0) {
            NSManagedObject *obj = [results firstObject];
            
            [obj setValuesForKeysWithDictionary:blockModel.getObjInfoDic];
            [self.coreDataApi updateEntity:^{
                if (success) {
                    success();
                }
            } fail:^(NSError *error) {
                if (fail) {
                    fail(error);
                }
            }];
        }
    } fail:^(NSError *error) {
        if (fail) {
            fail(error);
        }
    }];
}

#pragma mark - -- 删除一条上传记录
- (void)deleteEnoteModel:(Enote *)model success:(void(^)(void))success fail:(void(^)(NSError *error))fail
{
    NSString * filterStr = [NSString stringWithFormat:@"creatTimeOnly = %lld",model.creatTimeOnly];

    [self.coreDataApi readEntity:nil ascending:YES filterStr:filterStr success:^(NSArray *results) {
        if (results.count>0) {
            NSManagedObject *obj = [results firstObject];
            [self.coreDataApi deleteEntity:obj success:^{
                if (success) {
                    success();
                }
            } fail:^(NSError *error) {
                if (fail) {
                    fail(error);
                }
            }];
        }
    } fail:^(NSError *error) {
        if (fail) {
            fail(error);
        }
    }];
}

#pragma mark - -- 删除所有上传记录
- (void)deleteAllDataModel:(void(^)(void))success fail:(void(^)(NSError *error))fail
{
    [self.coreDataApi readEntity:nil ascending:YES filterStr:nil success:^(NSArray *results) {
        for (NSManagedObject *obj in results){
            [self.coreDataApi deleteEntity:obj success:^{
                if (success) {
                    success();
                }
            } fail:^(NSError *error) {
                if (fail) {
                    fail(error);
                }
            }];
        }
    } fail:^(NSError *error) {
        if (fail) {
            fail(error);
        }
    }];
}

#pragma mark - -- 查询所有上传记录
- (void)readAllDataModel:(void(^)(NSArray *modelArray))success fail:(void(^)(NSError *error))fail
{
    [self.coreDataApi readEntity:nil ascending:YES filterStr:nil success:^(NSArray *results) {
//        NSMutableArray *finishArray = [NSMutableArray array];
//        for (NSManagedObject *obj in results) {
        
            
            // 获取数据库中各个键值的值
//            model.content = [obj valueForKey:@"content"];
//            model.state   = [[obj valueForKey:@"state"] longLongValue];
//            model.time = [[obj valueForKey:@"time"] longLongValue];
//            model.creatTimeOnly = [[obj valueForKey:@"creatTimeOnly"] longLongValue];
//            [finishArray addObject:model];
           
            
//        }
        if (success) {
            success(results);
        }
    } fail:^(NSError *error) {
        if (fail) {
            fail(error);
        }
    }];
}

@end
